package com.wm.blog_common.result;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;

import java.io.Serializable;

/***
 * @ClassName: Result
 * @Description:
 * @Author: wm_yu
 * @Create_time: 11:05 2020-3-4
 */
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private Integer code;
    private String msg;
    private T data;

    public Result() {
    }

    public Result(CommonResultInfoEnum e) {
        this.code = e.getCode();
        this.msg = e.getMsg();
    }

    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Integer getCode() {
        return this.code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> Result<T> success(T data) {
        Result result = new Result(CommonResultInfoEnum.SUCCESS);
        result.setData(data);
        return result;
    }

    public static Result success() {
        return new Result(CommonResultInfoEnum.SUCCESS);
    }

    public static Result failure(String msg) {
        Result result = new Result(CommonResultInfoEnum.ERROR);
        result.setMsg(msg);
        return result;
    }

    public static Result failure(Integer code,String msg) {
        Result result = new Result(code,msg);
        result.setMsg(msg);
        return result;
    }

    public static Result failure() {
        return new Result(CommonResultInfoEnum.ERROR);
    }
    
    @JSONField(
            serialize = false
    )
    public boolean isSuccess() {
        return CommonResultInfoEnum.SUCCESS.getCode().equals(this.code);
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }
}
